package com.panfeng.xcloud.gateway.filters;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Filter 是否应该执行命令
 *
 * @author xiaobo
 * @date 2018-03-15
 */
public enum FilterCommand {

	YES(Boolean.TRUE), NO(Boolean.FALSE);

	private static final Logger logger = LoggerFactory.getLogger(FilterCommand.class);

	public final static String FILTERCOMMAND_PREFIX = "xdcloud-";

	public final static String FILTERCOMMAND_NAME = FILTERCOMMAND_PREFIX+ "FilterCommand";

	FilterCommand() {
	}

	FilterCommand(Boolean command) {
		this.command = command;
	}

	private Boolean command;

	public Boolean getCommand() {
		return command;
	}

	public void setCommand(Boolean command) {
		this.command = command;
	}

	public static Boolean isFilter(RequestContext ctx, ZuulFilter zuulFilter) {
		Object object = ctx.get(FILTERCOMMAND_NAME);
		if (null != object && object instanceof FilterCommand) {
			FilterCommand filterCommand = (FilterCommand) object;
			if (FilterCommand.NO.getCommand() == filterCommand.getCommand()) {
				logger.info(">>>> 不应该执行该Filter:{}的生命周期方法run，命令：{} <<<<",zuulFilter.getClass().getName(),FilterCommand.NO.getCommand());
				return FilterCommand.NO.getCommand();
			}
		}
		logger.info(">>>> 应该执行该Filter:{}的生命周期方法run ，命令：{}<<<<",zuulFilter.getClass().getName(),FilterCommand.YES.getCommand());
		return FilterCommand.YES.getCommand();
	}

}
